package jp.sourceforge.akjrcp.psp;

import java.util.ArrayList;
import java.util.List;



import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

public class PSPMovieSettingTableViewer extends TableViewer{
	private List movieSettingList;
	public PSPMovieSettingTableViewer(Composite parent) {
		super(parent);
		List list = new ArrayList();
		
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		setMovieSettingList(list);
	}
	
	public static List loadList(String value){
		List list=new ArrayList();
		PSPMovieSetting[] settings = PSPMovieSettingTableViewer.toPSPMovieSettings(value);
		for (int i = 0; i < settings.length; i++) {
			list.add(settings[i]);
		}
		return list;
		//refresh();
	}

	public String toLabel(PSPMovieSetting setting){
		String label="";
		if(setting.getVideoCodec()==PSPMovieSetting.XVID){
			label+="XVID";
		}else{
			label+="H264";
		}
		if(setting.isHighQuality()){
			label+="(HQ)";
		}
		label+=" ";
		if(setting.getVideoBitrate()>0){
			label+=setting.getVideoBitrate()+"kbps";
			label+=" ";
		}
		if(setting.getVideoSize()!=null){
			label+=setting.getVideoSize();
			label+=" ";
		}
		if(setting.getVideoRate()>0){
			label+=setting.getVideoRate()+"fps";
			label+=" ";
		}
		
		
		
		if(setting.getAudioBitrate()>0){
			label+=setting.getAudioBitrate()+"kbps";
			label+=" ";
		}
		
		if(setting.getAudioChannel()>0){
			label+=setting.getAudioChannel()+"ch";
			label+=" ";
		}
		
		if(setting.getAudioRate()>0){
			label+=setting.getAudioRate()+"hz";
			label+=" ";
		}
		
		if(setting.getComment()!=null && setting.getComment().length()>0){
			label+="("+setting.getComment()+")";
			label+=" ";
		}
		
		//audio info
		return label;
	}
	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {

			return toLabel((PSPMovieSetting) obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return null;
		}

	}
	
	public static PSPMovieSetting[] toPSPMovieSettings(String lines) {
		if (lines.length() == 0) {
			return new PSPMovieSetting[0];
		}
		String line[] = lines.split("\n");
		PSPMovieSetting[] infos = new PSPMovieSetting[line.length];
		for (int i = 0; i < infos.length; i++) {
			infos[i] = toPSPMovieSetting(line[i]);
		}
		return infos;
	}
	

	private static PSPMovieSetting toPSPMovieSetting(String text) {

		/*
		 * private int codec; private String size; //size must be 16x private
		 * int videoBitrate; //maybe xvid max is 768?
		 * 
		 * private double videoRate; private String aspectRatio; private boolean
		 * highQuality;
		 * 
		 * private int audioBitrate; private int audioChannel; private int
		 * audioRate; //k
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 */

		PSPMovieSetting setting = new PSPMovieSetting();
		String[] pairs = text.split("\t");
		for (int i = 0; i < pairs.length; i++) {
			if (pairs[i].indexOf("=") != -1) {
				String[] key_value = pairs[i].split("=");
				if(key_value.length==1){ //it happend,when it like that "key_value="
					//error
					//PSPSyncView.log.info(key_value[0]);
					if(!key_value[0].equals("comment")){
					PSPSyncView.log.warn(key_value[0]);
					}
					
				}
				else if (key_value[0].equals("codec")) {
					setting.setVideoCodec(Integer.parseInt(key_value[1]));
				} else if (key_value[0].equals("highquality")) {
					setting.setHighQuality(Boolean.parseBoolean(key_value[1]));
				} else if (key_value[0].equals("size")) {
					setting.setVideoSize(key_value[1]);
				} else if (key_value[0].equals("videobitrate")) {
					setting.setVideoBitrate(Integer.parseInt(key_value[1]));
				} else if (key_value[0].equals("videorate")) {
					setting.setVideoRate(Double.parseDouble(key_value[1]));
				} else if (key_value[0].equals("aspectratio")) {
					setting.setAspectRatio(key_value[1]);
				} else if (key_value[0].equals("audiobitrate")) {
					setting.setAudioBitrate(Integer.parseInt(key_value[1]));
				} else if (key_value[0].equals("audiorate")) {
					setting.setAudioRate(Integer.parseInt(key_value[1]));
				} else if (key_value[0].equals("audiochannel")) {
					setting.setAudioChannel(Integer.parseInt(key_value[1]));
				}else if (key_value[0].equals("comment")) {
					setting.setComment(key_value[1]);
				}
			} else {
				// maybe error
				PSPSyncView.log.warn("wrongpair:"+pairs[i]);
			}
		}
		return setting;
	}
	

	public static String toStoreString(PSPMovieSetting setting) {

		String str = "";
		str += "codec" + "=" + setting.getVideoCodec();
		str += "\t";

		str += "highquality" + "=" + setting.isHighQuality();
		str += "\t";

		if (setting.getVideoSize() != null) {
			str += "size" + "=" + setting.getVideoSize();
			str += "\t";
		}

		if (setting.getVideoBitrate() > 0) {
			str += "videobitrate" + "=" + setting.getVideoBitrate();
			str += "\t";
		}

		if (setting.getVideoRate() > 0) {
			str += "videorate" + "=" + setting.getVideoRate();
			str += "\t";
		}

		if (setting.getAspectRatio() != null) {
			str += "aspectratio" + "=" + setting.getAspectRatio();
			str += "\t";
		}

		if (setting.getAudioBitrate() > 0) {
			str += "audiobitrate" + "=" + setting.getAudioBitrate();
			str += "\t";
		}

		if (setting.getAudioRate() > 0) {
			str += "audiorate" + "=" + setting.getAudioRate();
			str += "\t";
		}

		if (setting.getAudioChannel() > 0) {
			str += "audiochannel" + "=" + setting.getAudioChannel();
			str += "\t";
		}
		
		if (setting.getComment() != null) {
			str += "comment" + "=" + setting.getComment();
			str += "\t";
		}

		return str;
	}


	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {

		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {

			return ((List) parent).toArray();
		}
	}

	public List getMovieSettingList() {
		return movieSettingList;
	}

	public void setMovieSettingList(List movieSettingList) {
		this.movieSettingList = movieSettingList;
		this.setInput(movieSettingList);
	}
}
